define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Employeemanager, Auth, GoPage, GoDialog, $timeout, $ionicSlideBoxDelegate, $ionicModal) {

		//底部按钮修改
		$scope.addbtn = "添加店员";
		//滑动切换
		$scope.selectTab = 0;
		$scope.activeTab = function(index) {
			$scope.selectTab = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
			if(index == 0) {
				$scope.addbtn = "添加店员";
			} else {
				$scope.addbtn = "添加角色";
			}

		};
		//滑动切换
		$scope.slideChanged = function(index) {
			$scope.selectTab = index;
		};

		//禁止左右滑动
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};

		//跳转页面
		$scope.onshow_addemployee = function(id) {
			if($scope.selectTab == 0) {
				GoPage.go_states("employeemanager_add", {
					"id": id
				});
			} else {
				GoPage.go_states("employeemanager_rights_add", {
					"id": id
				});
			}

		}
		//跳转到视频编辑页面
		$scope.onshow_addrights = function() {
			GoPage.go_states("employeemanager_rights_add");
		}

		$scope.onshow_edit = function(id) {
			GoPage.go_states("employeemanager_rights_add", {
				"id": id
			});
		}

		//加载数据
		$scope.init_data = function() {
			$scope.addbtn = "添加店员";
			Employeemanager.assistente_list(1, function(data) {
				if(data.result) {
					$scope.list = data.result;
				}
			});
			Employeemanager.part_list(1, 10, function(data) {
				$scope.part_list = data.role;
			});
			Employeemanager.part_list(1, 50, function(data) {
				$scope.part_lists = data.role;
			});
		};
		//$scope.init_data(); //必须在定义之后
		$scope.init_data_auth = function() {
			//回调产生
			var infos = Auth.get_all_user_auth(function(isHave, items) {
				//授权
				//console.log(isHave)
				//console.log(items)
				if(items['角色管理']) {
					$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(1);
				}
				//授权
				if(items['店员管理']) {
					$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(0);
				}
			});
		};
		//删除数据
		$scope.deletepart = function(id, index) {
			GoDialog.confirm("提示", "确认要删除该店员么？", function() {
					Employeemanager.part_delete(id, function(data) {
							if(data.success) { //数据库里删除成功，才能在前台删除
								GoDialog.toast("删除成功！");
								$scope.list.member.splice(index, 1);
							} else {
								GoDialog.toast(data.msg);
							}

							})
					})
			}
		//删除数据
		$scope.deletepart_right = function(id) {
			GoDialog.confirm("提示", "确认要删除该角色么？", function() {
				Employeemanager.rights_delete(id, function(data) {
					if(data.success) {
						var i = 0;
						var n = -1;
						for(var o in $scope.part_list) {
							if($scope.part_list[o].id == id) {
								n = i;
								break;
							}
							i++;
						}
						if(n != -1) {
							$scope.part_list.splice(n, 1);
						}
						GoDialog.toast("删除成功！");

					} else {
						GoDialog.toast(data.msg);
					}
				})
			})
			//console.log(id);
		}
		//下拉刷新
		$scope.input = new Object();
		$scope.input.nPage = 1;
		$scope.input.nPage_right = 1;
		$scope.isLoad = false; //可以加载
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			//防止重复加载
			$scope.isLoad = true;
			if($scope.selectTab == 0) {
				Employeemanager.assistente_list(1, function(data) {
						if(data.result != null) {
							$scope.list = data.result;;
						} else {
							$scope.list = new Object();
						}
						$scope.input.nPage = 1;
					},
					function() {
						$scope.$broadcast('scroll.refreshComplete');
						$timeout(function() {
							$scope.isLoad = false;
						}, 1500);
					});
			} else {
				Employeemanager.part_list(1, 10, function(data) {
						if(data != null) {
							$scope.part_list = data.role;
						} else {
							$scope.part_list = new Array();
						}
						$scope.input.nPage_right = 1;
					},
					function() {
						$scope.$broadcast('scroll.refreshComplete');
						$timeout(function() {
							$scope.isLoad = false;
						}, 1500);
					});
			}
		};
		//加载更多
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;
			if($scope.selectTab == 0) {
				Employeemanager.assistente_list($scope.input.nPage + 1, function(data) {
						if(data.result != null) {
							if($scope.list.member != undefined) {
								$scope.list.member = $scope.list.member.concat(data.result.member);
								$scope.input.nPage += 1;
							} else {
								$scope.list = data.result;
							}
						}
					},
					function() {
						$scope.$broadcast('scroll.infiniteScrollComplete');
						$timeout(function() {
							$scope.isLoad = false;
						}, 1500);
					});
			} else {
				Employeemanager.part_list($scope.input.nPage_right + 1, 10, function(data) {
					if(data != null) {
						if($scope.part_list != undefined) {
							$scope.part_list = $scope.part_list.concat(data.role);
							$scope.input.nPage_right += 1;
						} else {
							$scope.part_list = data.role;
						}
					}
				}, function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);

				});
			}
		};

		//跳转后刷新页面  /*******慎用********/
		$scope.$on('$ionicView.beforeEnter', function() {
			///局部刷新，更新所需的字段
			$scope.init_data();
		});
		//跳转后刷新页面  /*******慎用********/
		$scope.$on('$ionicView.afterEnter', function() {
			///局部刷新，更新所需的字段
			$scope.init_data_auth();
		});

		$ionicModal.fromTemplateUrl("templates/modal/rebuildRole.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal = modal;
		});
		//打开
		$scope.member_id;
		$scope.openModal_tag = function(member_id, part_id) {
			for(var i = 0; i < $scope.part_lists.length; i++) {
				if($scope.part_lists[i].id == part_id) {
					$scope.part_lists[i].isSelected = true;
				} else {
					$scope.part_lists[i].isSelected = false;
				}
			}
			$scope.modal.show();
			$scope.member_id = member_id;
		}
		//隐藏
		$scope.closeModal_tag = function() {
			$scope.modal.hide();
		}
		//暂时
		$scope.selectTagId = function(index) {
			for(var i = 0; i < $scope.part_lists.length; i++) {
				$scope.part_lists[i].isSelected = false;
			}
			$scope.part_lists[index].isSelected = true;
		}
		//修改店员角色
		$scope.updatarole = function() {
			for(var i = 0; i < $scope.part_lists.length; i++) {
				if($scope.part_lists[i].isSelected) {
					Employeemanager.modify($scope.member_id, $scope.part_lists[i].id, function(data) {
						GoDialog.toast(data.msg);
						$scope.closeModal_tag();
						$scope.init_data();
					});
				}
			}

		}

	}

	controller.$inject = ['$scope', 'Employeemanager', 'Auth', 'GoPage', 'GoDialog', '$timeout', '$ionicSlideBoxDelegate', '$ionicModal'];

	return app.registerController("employeemanagerctrl", controller);
});